package com.epfresh.adapter;

import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.api.universalimageloader.core.DisplayImageOptions;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.universalimageloader.core.assist.ImageScaleType;
import com.epfresh.bean.MerchantEntity;

import multitype.ItemViewBinder;

/**
 * Author: Chen
 * Date: 2019/1/30
 * Desc:
 */
public class AreaStoreViewBinder extends ItemViewBinder<MerchantEntity.AreaBean.StoresBean, AreaStoreViewBinder.ViewHolder> {
    private OnMoreStoreClickListener moreStoreClickListener;

    public void setMoreStoreClickListener(OnMoreStoreClickListener moreStoreClickListener) {
        this.moreStoreClickListener = moreStoreClickListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View itemView = inflater.inflate(R.layout.item_merchant, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull final MerchantEntity.AreaBean.StoresBean item) {
        holder.img_recommend.setVisibility(View.GONE);

        if (item.getGroupName() != null) {//每组的第一个
            holder.rl_title.setVisibility(View.VISIBLE);
            holder.txt_title.setText(item.getGroupName());

            if (item.isHaveMore()) {
                holder.txt_more.setVisibility(View.VISIBLE);
                holder.img_more.setVisibility(View.VISIBLE);

                holder.txt_more.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (moreStoreClickListener != null) {
                            moreStoreClickListener.onMoreStoreClick(item.getAreaId());
                        }
                    }
                });
                holder.img_more.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (moreStoreClickListener != null) {
                            moreStoreClickListener.onMoreStoreClick(item.getAreaId());
                        }
                    }
                });
            } else {
                holder.txt_more.setVisibility(View.GONE);
                holder.img_more.setVisibility(View.GONE);
            }
        } else {
            holder.rl_title.setVisibility(View.GONE);
        }

        holder.ll_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (moreStoreClickListener != null) {
                    moreStoreClickListener.onItemClick(item);
                }
            }
        });

        ImageLoader.getInstance().displayImage(item.getHeaderImage(), holder.img_logo, options);
        holder.img_status.setVisibility(View.VISIBLE);
        switch (item.getRecType()) {
            case -1://休息中
                holder.img_status.setImageResource(R.mipmap.icon_resting);
                break;
            case 0://普通既没有
                holder.img_status.setVisibility(View.INVISIBLE);
                break;
            case 1://已购买
                holder.img_status.setImageResource(R.mipmap.icon_bought);
                break;
            case 2://已关注
                holder.img_status.setImageResource(R.mipmap.icon_attention);
                break;
        }
        holder.txt_merchant_name.setText(item.getName());
        holder.txt_order_count.setText("订单量" + item.getOrderCount());
        holder.txt_goods_count.setText("商品" + item.getOnShelvesProductCount());
        holder.txt_rating.setText("好评" + item.getGoodCommentsRate());
        holder.txt_wholesale_market.setVisibility(View.VISIBLE);
        holder.txt_wholesale_market.setText(item.getStall());

        if (item.isLast()) {
            holder.line_space.setVisibility(View.VISIBLE);
        } else {
            holder.line_space.setVisibility(View.GONE);
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private View line_space, rl_title,ll_item;
        private ImageView img_recommend, img_more, img_logo, img_status;
        private TextView txt_title, txt_more, txt_merchant_name, txt_order_count, txt_goods_count, txt_rating, txt_wholesale_market;

        ViewHolder(View itemView) {
            super(itemView);

            line_space = itemView.findViewById(R.id.line_space);
            rl_title = itemView.findViewById(R.id.rl_title);
            ll_item = itemView.findViewById(R.id.ll_item);
            img_recommend = itemView.findViewById(R.id.img_recommend);
            img_more = itemView.findViewById(R.id.img_more);
            img_logo = itemView.findViewById(R.id.img_logo);
            img_status = itemView.findViewById(R.id.img_status);
            txt_title = itemView.findViewById(R.id.txt_title);
            txt_more = itemView.findViewById(R.id.txt_more);
            txt_merchant_name = itemView.findViewById(R.id.txt_merchant_name);
            txt_order_count = itemView.findViewById(R.id.txt_order_count);
            txt_goods_count = itemView.findViewById(R.id.txt_goods_count);
            txt_rating = itemView.findViewById(R.id.txt_rating);
            txt_wholesale_market = itemView.findViewById(R.id.txt_wholesale_market);
        }
    }

    public interface OnMoreStoreClickListener {
        void onMoreStoreClick(String areaId);

        void onItemClick(MerchantEntity.AreaBean.StoresBean item);
    }

    private DisplayImageOptions options = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.mipmap.icon_fail_merchant) //设置图片在下载期间显示的图片
            .showImageForEmptyUri(R.mipmap.icon_fail_merchant)//设置图片Uri为空或是错误的时候显示的图片
            .showImageOnFail(R.mipmap.icon_fail_merchant)
            .cacheInMemory(true)// 设置下载的图片是否缓存在内存中
            .cacheOnDisk(true)// 设置下载的图片是否缓存在SD卡中
            .considerExifParams(true) // 是否考虑JPEG图像EXIF参数（旋转，翻转）
            .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)// 设置图片以如何的编码方式显示
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();

}
